package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.StateVertex;
import ru.amse.tsyganov.jumleditor.model.Transition;
import ru.amse.tsyganov.jumleditor.view.representation.TransitionView;

/*package*/ class NewTransitionCommand extends Command {
	
	private final TransitionView<Transition> transitionView;
	
	public NewTransitionCommand(
			TransitionView<Transition> transitionView) {
		
		if (transitionView == null) {
			throw new IllegalArgumentException();
		}
		
		this.transitionView = transitionView;
		StateVertex sourceStateVertex = transitionView.getModel().getSource();
		StateVertex targetStateVertex = transitionView.getModel().getTarget();
		
		for (Transition t : sourceStateVertex.getOutgoing()) {
			if (t.getTarget() == targetStateVertex) {
				throw new IllegalArgumentException();
			}
		}
	}
	
	@Override
	public void execute() {
		transitionView.getModelViewMap().getTransitionsMap().
			put(transitionView.getModel(), transitionView);
		
		transitionView.getModel().addBindings();
	}

	@Override
	public void unexecute() {
		transitionView.getModelViewMap().getTransitionsMap().
			remove(transitionView.getModel());
				
		transitionView.getModel().removeBindings();
		transitionView.getActivityGraphView().
			getSelectedItems().getSelectedTransitions().
				remove(transitionView);
	}
}
